package com.sicnu.cheer.leaugebar.managemodule.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sicnu.cheer.generalmodule.bean.JoinLeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.managemodule.R;
import com.sicnu.cheer.generalmodule.activity.BaseActivity;

import cn.bmob.v3.listener.UpdateListener;

public class ResumeDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView userHeadIV;
    private TextView userNameTV;
    private ImageView sexIV;
    private TextView sexTV;
    private TextView hometownTV;
    private TextView gradeTV;
    private TextView majorTV;
    private TextView phoneTV;
    private TextView qqTV;
    private TextView selfIntroduceTV;
    private ImageView callIV;
    private Button negativeBTN;
    private Button positiveBTN;
    private JoinLeagueBean resume;
    private boolean isApproval;
    private ProgressDialog progressDialog;
    private TextView leagueTV;

    @Override
    public int getContentViewId() {
        return R.layout.activity_resume_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        findViewById(R.id.back).setOnClickListener(this);
        userHeadIV = ((ImageView) findViewById(R.id.detail_userhead_iv));
        userNameTV = ((TextView) findViewById(R.id.detail_username_tv));
        sexIV = ((ImageView) findViewById(R.id.sex_iv));
        sexTV = ((TextView) findViewById(R.id.sex_tv));
        hometownTV = ((TextView) findViewById(R.id.hometown_tv));
        leagueTV = ((TextView) findViewById(R.id.league_tv));
        gradeTV = ((TextView) findViewById(R.id.grade_tv));
        majorTV = ((TextView) findViewById(R.id.major_tv));
        callIV = ((ImageView) findViewById(R.id.call_iv));
        callIV.setOnClickListener(this);
        phoneTV = ((TextView) findViewById(R.id.phone_tv));
        qqTV = ((TextView) findViewById(R.id.qq_tv));
        selfIntroduceTV = ((TextView) findViewById(R.id.self_introduce_tv));

        negativeBTN = ((Button) findViewById(R.id.negative_btn));
        positiveBTN = ((Button) findViewById(R.id.positive_btn));
        negativeBTN.setOnClickListener(this);
        positiveBTN.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        progressDialog = new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");

        resume = (JoinLeagueBean) intent.getSerializableExtra("resume");
        isApproval = intent.getBooleanExtra("isApproval",false);
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        MyUser myUser = resume.getMyUser();
        if (myUser!=null) {
            Glide.with(mThis).load(myUser.getHeadPic()).placeholder(R.mipmap.default_user_circle).into(userHeadIV);
            userNameTV.setText(myUser.getNick());
            sexIV.setImageResource(myUser.getSex().equals("女")?R.mipmap.female:R.mipmap.male);
            sexTV.setText(myUser.getSex());
            if (!StringUtils.isEmpty(myUser.getHometown())) {
                hometownTV.setText(myUser.getHometown().split("-")[1]);
            }
            gradeTV.setText(myUser.getGrade());
            majorTV.setText(myUser.getMajor());
            phoneTV.setText(myUser.getMobilePhoneNumber());
            qqTV.setText(myUser.getQq());
        }
        if (resume.getLeagueBean()!=null){
            leagueTV.setText(resume.getLeagueBean().getName());
        }
        selfIntroduceTV.setText(resume.getSelfIntroduce());
        if (resume.getPass()==0) {
            if (isApproval) {
                negativeBTN.setText("拒绝申请");
                positiveBTN.setText("同意申请");
            }
        }else {
            negativeBTN.setVisibility(View.GONE);
            positiveBTN.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            onBackPressed();
        }else if (v.getId()==R.id.positive_btn){
            perform(1);
        }else if (v.getId()==R.id.negative_btn){
            perform(2);
        }else if (v.getId()==R.id.call_iv){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + String.valueOf(resume.getMyUser().getMobilePhoneNumber())));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    /**
     * 执行同意或者拒绝申请的操作
     * @param state
     */
    private void perform(final int state) {
        progressDialog.show();
        JoinLeagueBean bean=new JoinLeagueBean();
        bean.setPass(state);
        bean.update(mThis, resume.getObjectId(), new UpdateListener() {
            @Override
            public void onSuccess() {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                UIHelper.ToastMessage(mThis,state==1?"同意申请成功":"拒绝申请成功");
                setResult(1);
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                UIHelper.ToastMessage(mThis,state==1?"同意申请失败:":"拒绝申请失败:"+s);
            }
        });
    }
}
